starter.controller('adCheckDetailCtrl', ['$rootScope','$timeout','$ionicPopup','$state', '$scope', 'Page',
    function($rootScope,$timeout,$ionicPopup,$state, $scope, Page) {

        $scope.transData = $state.params.transData;

        if($scope.transData == undefined || jQuery.isEmptyObject($scope.transData)) {
            $state.go('adCheck');
            return;
        }

        Page.ajaxPostQuiet('web/queryCurrentBillDetail.do', {resourceId : $scope.transData.resourceId}, function (resp) {
            $scope.billDetailData = resp.data;
            if(jQuery.isEmptyObject($scope.billDetailData)){
                $scope.showDetail = false;
            } else {
                $scope.showDetail = true;
            }
        });

        $scope.detail = {};
        $scope.detail.checkResult = "1";
        $scope.$watch('detail.file', function (newValue, oldValue, $scope) {
            if(newValue != undefined) {
                if(newValue.type != "jpg" && newValue.type != "jpeg" && newValue.type != "png"
                    && newValue.type != "bmp" && newValue.type != "gif") {
                    //window.webkit.messageHandlers.mobileAlert.postMessage({title:'警告',content:"请上传图片",action:false})
                    var alertPopup = $ionicPopup.alert({
                        title: '警告',
                        template: '请选择图片类型的文件!'
                    });
                    return;
                }
            }
        });

        resultCheck = function(obj,result) {
            $scope.detail.checkResult = result;
            if(result == "1") { //符合
                $(obj).css("border","1px solid #7BBFF9");
                $(obj).find("a").css("color","#6CB8F8");
                $(obj).next().css("border","1px solid #E7E7E7");
                $(obj).next().find("a").css("color","#959595");
            } else if(result == "0") {
                $(obj).css("border","1px solid #7BBFF9");
                $(obj).find("a").css("color","#6CB8F8");
                $(obj).prev().css("border","1px solid #E7E7E7");
                $(obj).prev().find("a").css("color","#959595");
            }
        }

        $scope.resCheckSubmit = function() {
            if($scope.detail.file == undefined) {
                var alertPopup = $ionicPopup.alert({
                    title : '警告',
                    template: ' <span style="position: relative;left: 30%">请先上传图片!</span>'
                });
                $timeout(function() {
                    alertPopup.close(); // 3秒后关闭弹窗
                }, 1500);
                return;
            }
            $scope.detail.checkResult = $('input[name="checkResult"]:checked').val();
            Page.ajaxPostQuiet('web/saveResCheckRecord.do', {file : $scope.detail.file,comment : $scope.detail.comment,
                resourceId : $scope.transData.resourceId, result : $scope.detail.checkResult,sourceType : '3',
                dataId : $scope.billDetailData.makeBillId}, function (resp) {
                var completePopup = $ionicPopup.alert({
                    title: '完成',
                    template: '提交成功!'
                });
                $timeout(function() {
                    completePopup.close(); // 1秒后关闭弹窗
                    //$state.go('adCheck',null,{reload : true});
                    if(window.js != undefined && window.js.mobileAlert != undefined) {
                        window.js.mobileBack("0"); //0 返回 1退出
                    } else {
                        window.webkit.messageHandlers.mobileBack.postMessage("0")//0 返回 1退出
                    }
                    return;
                }, 1500);

                completePopup.then(function(res) {
                    //$state.go('adCheck',null,{reload : true});
                    if(window.js != undefined && window.js.mobileAlert != undefined) {
                        window.js.mobileBack("0"); //0 返回 1退出
                    } else {
                        window.webkit.messageHandlers.mobileBack.postMessage("0")//0 返回 1退出
                    }
                    return;
                });
            });
        }

        $scope.viewMediaImg = function(path) {
            $scope.Url = $rootScope.nginxPath + path;
            $scope.bigImage = true;

            var transformStyle = $("div[nav-view='active'] div.scroll").css("transform");
            var wantVal = -parseInt(transformStyle.substring(transformStyle.lastIndexOf(",") + 2,transformStyle.indexOf(")")));

            $scope.imgDivStyle = {
                "position": "absolute",
                "top": wantVal + "px",
                "left": "0",
                "z-index": "10",
                "width": "100%",
                "height": "100%"
            }
            $scope.imgStyle = {
                "position": "relative",
                "top": "5%",
                "left": "56%",
                "z-index": "10",
                "display": "block",
                "margin-top": "18px",
                "margin-left": "-165px",
                "height": "305px",
                "width": "280px",
                "box-shadow": "0 1px 3px rgba(0, 0, 0, 0.4)"
            }
        }

        $scope.hideBigImage = function () {
            $scope.bigImage = false;
        };
        $scope.indexTitle = "图片浏览";
        $scope.bigImage = false;    //初始默认大图是隐藏的
        $scope.imageIndex = -1;//当前展示的图片

        //点击图片放大
        $scope.shouBigImage = function (imageName, imageIndex) {  //传递一个参数（图片的URl）
            $scope.imageIndex = imageIndex;
            $scope.Url = imageName;                   //$scope定义一个变量Url，这里会在大图出现后再次点击隐藏大图使用
            $scope.bigImage = true;                   //显示大图
        }


        $scope.showBig = function(path) {
            $scope.Url = $rootScope.nginxPath + path;
            $scope.bigImage = true;

            var transformStyle = $("div[nav-view='active'] div.scroll").css("transform");
            var wantVal = -parseInt(transformStyle.substring(transformStyle.lastIndexOf(",") + 2,transformStyle.indexOf(")")));

            $scope.imgDivStyle = {
                "position": "absolute",
                "top": wantVal + "px",
                "left": "0",
                "z-index": "10",
                "width": "100%",
                "height": "100%"
            }
            $scope.imgStyle = {
                "position": "relative",
                "top": "5%",
                "left": "56%",
                "z-index": "10",
                "display": "block",
                "margin-top": "18px",
                "margin-left": "-165px",
                "height": "305px",
                "width": "280px",
                "box-shadow": "0 1px 3px rgba(0, 0, 0, 0.4)",
                "background-color" : "#F9F9F9"
            }
        }

}]);
